$gacutilFilePath = Resolve-Path -Path (join-path (Get-ScriptDir) "libs\gacutil.exe")

#$servers | % {Copy-Item -Path $gacutilFilePath -Destination "\\$_\c$"}

Set-Alias -name gacutil -value $gacutilFilePath

function Copy-GacUtilToServer ($server)
{
	if(-not (Test-Path "\\$server\c$\gacutil.exe"))
	{
		Copy-Item -Path $gacutilFilePath -Destination "\\$server\c$"
		return $gacutil = 'c:\gacutil.exe'
	}
	elseif (Test-Path "\\$server\c$\gacutil.exe")
	{
		return $gacutil = 'c:\gacutil.exe'
	}
	else
	{
		Throw "Unable to locate gacutil.exe at `'\\$server\c$\gacutil.exe`'"
	}
}

function UnGac-Dlls ([array]$dllarray, $server)
{
	$isLocal = Is-LocalMachine $server
	if(-not ($isLocal))
	{
		$gacutil = Copy-GacUtilToServer $server
	}
	foreach($file in $dllarray)
	{
		$dllname = [system.IO.Path]::GetFileNameWithoutExtension($file)
		$msg = "Ungac $file"
 		if (Should-Process $msg ([REF]$AllAnswer) "" -Verbose:$Verbose -Confirm:$Confirm -Whatif:$Whatif)
 		{
			log-message $msg -fore green
			if($isLocal)
			{
				gacutil /u $dllName
			}
			else
			{
				psexec "\\$server" $gacutil /u $dllName 2>$null
			}
		}
	}
}

function Gac-Dlls ([array]$dllarray, $server)
{
	$isLocal = Is-LocalMachine $server
	if(-not ($isLocal))
	{
		$gacutil = Copy-GacUtilToServer $server
	}
	foreach($file in $dllarray)
	{
		$msg = "Gac $file"
		if (Should-Process $msg ([REF]$AllAnswer) "" -Verbose:$Verbose -Confirm:$Confirm -Whatif:$Whatif)
		{
			log-message $msg -fore green
			if($isLocal)
			{
				gacutil /i $file.fullname
			}
			else
			{
				psexec "\\$server" $gacutil /i $file.fullname 2>$null
			}
		}
	}
}

